home *** CD-ROM | disk | FTP | other *** search
/ Aminet 30 / Aminet 30 (1999)(Schatztruhe)[!][Apr 1999].iso / Aminet / gfx / fract / FlashMandel.lha / FlashMandel / Sources / Modules / ColorWindow.c next >
C/C++ Source or Header  |  1999-01-22  |  36KB  |  882 lines

  1. /*******************************************************************************
  2. **
  3. **  Coded by Dino Papararo                 12-Sep-1998
  4. **
  5. *******************************************************************************/
  6.  
  7. #define __USE_SYSBASE
  8.  
  9. #include <math.h>
  10. #include <string.h>
  11. #include <exec/types.h>
  12. #include <exec/execbase.h>
  13. #include <intuition/intuition.h>
  14. #include <intuition/gadgetclass.h>
  15. #include <proto/exec.h>
  16. #include <proto/intuition.h>
  17. #include <proto/graphics.h>
  18. #include <proto/gadtools.h>
  19. #include <proto/utility.h>
  20.  
  21. #define MAX(a,b)    ((a)>(b)?(a):(b))
  22.  
  23. #define PALETTE      (0L)
  24. #define ACCEPT       (1L)
  25. #define RESET        (2L)
  26. #define CANCEL       (3L)
  27. #define RED          (4L)
  28. #define GREEN        (5L)
  29. #define BLUE         (6L)
  30. #define COPY         (7L)
  31. #define SWAP         (8L)
  32. #define SPREAD       (9L)
  33. #define UNDO         (10L)
  34. #define INVERT       (11L)
  35.  
  36. #define STARTPEN     (4L)
  37.  
  38. #define MINVALUE     (0L)
  39. #define MAXVALUE     (255L)
  40. #define DELTA        (10L)
  41.  
  42. BOOL ModifyPalette  (struct Window *,WORD,WORD,ULONG *);
  43. VOID KeepPalette    (struct Window *);
  44. VOID Copy           (struct Window *,ULONG);
  45. VOID Paste          (struct Window *,const ULONG);
  46. VOID Swap           (struct Window *,const ULONG,const ULONG);
  47. BOOL Spread         (struct Window *,const ULONG,const ULONG);
  48. VOID InvertPalette  (struct Window *,ULONG,ULONG);
  49.  
  50. IMPORT struct ExecBase *SysBase;
  51. IMPORT BOOL CheckEHB (ULONG);
  52.  
  53. ULONG COLOR_RGB [3L * 256L + 2L],UNDO_RGB [3L * 256L + 2L],COPY_RGB [3L];
  54.  
  55. struct NewGadget BUTTON_GAD,SLIDER_GAD,PALETTE_GAD;
  56.  
  57. BOOL ModifyPalette (struct Window *Win,WORD LeftEdge,WORD TopEdge,ULONG *Palette32)
  58. {
  59. struct Window *ColorWin;
  60.  
  61. struct IntuiMessage *Message;
  62.  
  63. struct Gadget *GadList = NULL,*MyButtonGad,*MyPaletteGad;
  64.  
  65. struct Gadget *RedSliderGad,*GreenSliderGad,*BlueSliderGad,*MyGad = NULL;
  66.  
  67. BOOL Copy_Msg = FALSE,Swap_Msg = FALSE,Spread_Msg = FALSE,Exit = FALSE;
  68.  
  69. ULONG SelectedPen = STARTPEN,OldPen = NULL,NewPen;
  70.  
  71. UWORD MyCode = 0;
  72.  
  73. ULONG MyClass = NULL,Colors,ColorBase = 3L * STARTPEN + 1L;
  74.  
  75. ULONG RedLevel,GreenLevel,BlueLevel;
  76.  
  77. CPTR *VInfo;
  78.  
  79.   Colors = 1L << Win->RPort->BitMap->Depth;
  80.  
  81.   if (Colors < 2L) return FALSE;
  82.  
  83.   VInfo = GetVisualInfo (Win->WScreen,TAG_END);
  84.  
  85.   if (! VInfo) return FALSE;
  86.  
  87.   if (CheckEHB (GetVPModeID (ViewPortAddress (Win)))) Colors = 32L;
  88.  
  89.   COLOR_RGB [0L] = UNDO_RGB [0L] = Colors << 16L;
  90.  
  91.   COLOR_RGB [3L * Colors + 1L] = UNDO_RGB [3L * Colors + 1L] = NULL;
  92.  
  93.   COPY_RGB [0L] = COPY_RGB [1L] = COPY_RGB [2L] = NULL;
  94.  
  95.   GetRGB32 (ViewPortAddress (Win)->ColorMap,0L,Colors,&COLOR_RGB [1L]);
  96.  
  97.   GetRGB32 (ViewPortAddress (Win)->ColorMap,0L,Colors,&UNDO_RGB [1L]);
  98.  
  99.   RedLevel   =  (COLOR_RGB [ColorBase]      >> 24L);
  100.  
  101.   GreenLevel =  (COLOR_RGB [ColorBase + 1L] >> 24L);
  102.  
  103.   BlueLevel  =  (COLOR_RGB [ColorBase + 2L] >> 24L);
  104.  
  105.   MyButtonGad = CreateContext (&GadList);
  106.  
  107.   BUTTON_GAD.ng_VisualInfo = PALETTE_GAD.ng_VisualInfo = SLIDER_GAD.ng_VisualInfo = VInfo;
  108.  
  109.   BUTTON_GAD.ng_LeftEdge   = 266;
  110.  
  111.   BUTTON_GAD.ng_TopEdge    = 10;
  112.  
  113.   BUTTON_GAD.ng_Width      = 90;
  114.  
  115.   BUTTON_GAD.ng_Height     = (Win->WScreen->Font->ta_YSize * 3) >> 1;
  116.  
  117.   BUTTON_GAD.ng_GadgetText = "C_opy";
  118.  
  119.   BUTTON_GAD.ng_GadgetID   = COPY;
  120.  
  121.   MyButtonGad = CreateGadget (BUTTON_KIND,MyButtonGad,&BUTTON_GAD,GT_Underscore,'_',TAG_END);
  122.  
  123.   BUTTON_GAD.ng_TopEdge   += (10 + BUTTON_GAD.ng_Height);
  124.  
  125.   BUTTON_GAD.ng_GadgetText = "S_wap";
  126.  
  127.   BUTTON_GAD.ng_GadgetID   = SWAP;
  128.  
  129.   MyButtonGad = CreateGadget (BUTTON_KIND,MyButtonGad,&BUTTON_GAD,GT_Underscore,'_',TAG_END);
  130.  
  131.   BUTTON_GAD.ng_TopEdge   += (10 + BUTTON_GAD.ng_Height);
  132.  
  133.   BUTTON_GAD.ng_GadgetText = "_Spread";
  134.  
  135.   BUTTON_GAD.ng_GadgetID   = SPREAD;
  136.  
  137.   MyButtonGad = CreateGadget (BUTTON_KIND,MyButtonGad,&BUTTON_GAD,GT_Underscore,'_',TAG_END);
  138.  
  139.   BUTTON_GAD.ng_TopEdge   += (10 + BUTTON_GAD.ng_Height);
  140.  
  141.   BUTTON_GAD.ng_GadgetText = "_Invert";
  142.  
  143.   BUTTON_GAD.ng_GadgetID   = INVERT;
  144.  
  145.   MyButtonGad = CreateGadget (BUTTON_KIND,MyButtonGad,&BUTTON_GAD,GT_Underscore,'_',TAG_END);
  146.  
  147.   BUTTON_GAD.ng_TopEdge   += (10 + BUTTON_GAD.ng_Height);
  148.  
  149.   BUTTON_GAD.ng_GadgetText = "_Undo";
  150.  
  151.   BUTTON_GAD.ng_GadgetID   = UNDO;
  152.  
  153.   MyButtonGad = CreateGadget (BUTTON_KIND,MyButtonGad,&BUTTON_GAD,GT_Underscore,'_',TAG_END);
  154.  
  155.   PALETTE_GAD.ng_LeftEdge   = 10;
  156.  
  157.   PALETTE_GAD.ng_TopEdge    = 10;
  158.  
  159.   PALETTE_GAD.ng_Width      = 250;
  160.  
  161.   PALETTE_GAD.ng_Height     = MAX (200,BUTTON_GAD.ng_TopEdge + BUTTON_GAD.ng_Height);
  162.  
  163.   PALETTE_GAD.ng_GadgetID   = PALETTE;
  164.  
  165.   MyPaletteGad = CreateGadget (PALETTE_KIND,MyButtonGad,&PALETTE_GAD,GTPA_Color,4,GTPA_IndicatorWidth,10,GTPA_NumColors,(UWORD) Colors,TAG_END);
  166.  
  167.   SLIDER_GAD.ng_LeftEdge    = 25;
  168.  
  169.   SLIDER_GAD.ng_TopEdge     = MAX (PALETTE_GAD.ng_TopEdge + PALETTE_GAD.ng_Height,BUTTON_GAD.ng_TopEdge + BUTTON_GAD.ng_Height) + 10;
  170.  
  171.   SLIDER_GAD.ng_Width       = 290;
  172.  
  173.   SLIDER_GAD.ng_Height      = Win->WScreen->Font->ta_YSize;
  174.  
  175.   SLIDER_GAD.ng_GadgetText  = "R";
  176.  
  177.   SLIDER_GAD.ng_GadgetID    = RED;
  178.  
  179.   RedSliderGad = CreateGadget (SLIDER_KIND,MyPaletteGad,&SLIDER_GAD,GA_RelVerify,TRUE,GTSL_Max,MAXVALUE,GTSL_Level,(WORD) RedLevel,GTSL_LevelFormat,"%03ld",GTSL_MaxLevelLen,4,GTSL_LevelPlace,PLACETEXT_RIGHT,TAG_END);
  180.  
  181.   SLIDER_GAD.ng_TopEdge    += (8 + SLIDER_GAD.ng_Height);
  182.  
  183.   SLIDER_GAD.ng_GadgetText  = "G";
  184.  
  185.   SLIDER_GAD.ng_GadgetID    = GREEN;
  186.  
  187.   GreenSliderGad = CreateGadget (SLIDER_KIND,RedSliderGad,&SLIDER_GAD,GA_RelVerify,TRUE,GTSL_Max,MAXVALUE,GTSL_Level,(WORD) GreenLevel,GTSL_LevelFormat,"%03ld",GTSL_MaxLevelLen,4,GTSL_LevelPlace,PLACETEXT_RIGHT,TAG_END);
  188.  
  189.   SLIDER_GAD.ng_TopEdge    += (8 + SLIDER_GAD.ng_Height);
  190.  
  191.   SLIDER_GAD.ng_GadgetText  = "B";
  192.  
  193.   SLIDER_GAD.ng_GadgetID    = BLUE;
  194.  
  195.   BlueSliderGad = CreateGadget (SLIDER_KIND,GreenSliderGad,&SLIDER_GAD,GA_RelVerify,TRUE,GTSL_Max,MAXVALUE,GTSL_Level,(WORD) BlueLevel,GTSL_LevelFormat,"%03ld",GTSL_MaxLevelLen,4,GTSL_LevelPlace,PLACETEXT_RIGHT,TAG_END);
  196.  
  197.   BUTTON_GAD.ng_LeftEdge   = 10;
  198.  
  199.   BUTTON_GAD.ng_TopEdge    =  SLIDER_GAD.ng_TopEdge + SLIDER_GAD.ng_Height + 20;
  200.  
  201.   BUTTON_GAD.ng_GadgetText = "_Accept";
  202.  
  203.   BUTTON_GAD.ng_GadgetID   = ACCEPT;
  204.  
  205.   MyButtonGad = CreateGadget (BUTTON_KIND,BlueSliderGad,&BUTTON_GAD,GT_Underscore,'_',TAG_END);
  206.  
  207.   BUTTON_GAD.ng_LeftEdge  += (38 + BUTTON_GAD.ng_Width);
  208.  
  209.   BUTTON_GAD.ng_GadgetText = "Reset";
  210.  
  211.   BUTTON_GAD.ng_GadgetID   = RESET;
  212.  
  213.   MyButtonGad = CreateGadget (BUTTON_KIND,MyButtonGad,&BUTTON_GAD,TAG_END);
  214.  
  215.   BUTTON_GAD.ng_LeftEdge  += (38 + BUTTON_GAD.ng_Width);
  216.  
  217.   BUTTON_GAD.ng_GadgetText = "_Cancel";
  218.  
  219.   BUTTON_GAD.ng_GadgetID   = CANCEL;
  220.  
  221.   MyButtonGad = CreateGadget (BUTTON_KIND,MyButtonGad,&BUTTON_GAD,GT_Underscore,'_',TAG_END);
  222.  
  223.   if (! MyButtonGad)
  224.   {
  225.      FreeGadgets (GadList);
  226.  
  227.      FreeVisualInfo (VInfo);
  228.  
  229.      return FALSE;
  230.   }
  231.  
  232.   ColorWin = OpenWindowTags (0,WA_Left,LeftEdge,
  233.                                WA_Top,TopEdge,
  234.                                WA_Width,BUTTON_GAD.ng_LeftEdge + BUTTON_GAD.ng_Width + 25,
  235.                                WA_Height,BUTTON_GAD.ng_TopEdge + BUTTON_GAD.ng_Height + 35,
  236.                                WA_Title,"Palette requester",
  237.                                WA_ScreenTitle,"Modify palette...",
  238.                                WA_CustomScreen,Win->WScreen,
  239.                                WA_IDCMP,IDCMP_CLOSEWINDOW|IDCMP_REFRESHWINDOW|IDCMP_VANILLAKEY|IDCMP_GADGETDOWN|BUTTONIDCMP|SLIDERIDCMP|PALETTEIDCMP,
  240.                                WA_Flags,WFLG_ACTIVATE|WFLG_DRAGBAR|WFLG_SIMPLE_REFRESH|WFLG_RMBTRAP|WFLG_GIMMEZEROZERO,
  241.                                WA_Gadgets,GadList,
  242.                                TAG_END);
  243.  
  244.   if (! ColorWin)
  245.   {
  246.      FreeGadgets (GadList);
  247.  
  248.      FreeVisualInfo (VInfo);
  249.  
  250.      return FALSE;
  251.   }
  252.  
  253.   GT_RefreshWindow (ColorWin,NULL);
  254.  
  255.   do { WaitPort (ColorWin->UserPort);
  256.  
  257.        if (Message = (struct IntuiMessage *) GT_GetIMsg (ColorWin->UserPort))
  258.        {
  259.           MyGad   = (struct Gadget *) Message->IAddress;
  260.  
  261.           MyClass = Message->Class;
  262.  
  263.           MyCode  = Message->Code;
  264.  
  265.           GT_ReplyIMsg ((struct IntuiMessage *) Message);
  266.        }
  267.  
  268.        switch (MyClass)
  269.        {
  270.               case IDCMP_REFRESHWINDOW : GT_BeginRefresh (ColorWin);
  271.  
  272.                                          GT_EndRefresh   (ColorWin,TRUE);
  273.  
  274.                                          break;
  275.  
  276.               case IDCMP_VANILLAKEY    : switch (MyCode)
  277.                                          {
  278.                                                  case 'a' :
  279.                                                  case 'A' : GetRGB32 (ViewPortAddress (ColorWin)->ColorMap,0L,Colors,&Palette32 [1L]);
  280.  
  281.                                                             Exit = TRUE;
  282.  
  283.                                                             break;
  284.  
  285.                                                  case 'c' :
  286.                                                  case 'C' : LoadRGB32 (ViewPortAddress (ColorWin),Palette32);
  287.  
  288.                                                             Exit = TRUE;
  289.  
  290.                                                             break;
  291.  
  292.                                                  case 'o' :
  293.                                                  case 'O' : Copy (ColorWin,SelectedPen);
  294.  
  295.                                                             Copy_Msg = TRUE;
  296.  
  297.                                                             Swap_Msg = FALSE;
  298.  
  299.                                                             Spread_Msg = FALSE;
  300.  
  301.                                                             break;
  302.  
  303.                                                  case 'w' :
  304.                                                  case 'W' : OldPen = SelectedPen;
  305.  
  306.                                                             Copy_Msg = FALSE;
  307.  
  308.                                                             Swap_Msg = TRUE;
  309.  
  310.                                                             Spread_Msg = FALSE;
  311.  
  312.                                                             break;
  313.  
  314.                                                  case 's' :
  315.                                                  case 'S' : OldPen = SelectedPen;
  316.  
  317.                                                             Copy_Msg = FALSE;
  318.  
  319.                                                             Swap_Msg = FALSE;
  320.  
  321.                                                             Spread_Msg = TRUE;
  322.  
  323.                                                             break;
  324.  
  325.                                                  case 'u' :
  326.                                                  case 'U' : LoadRGB32 (ViewPortAddress (ColorWin),UNDO_RGB);
  327.  
  328.                                                             GetRGB32 (ViewPortAddress (ColorWin)->ColorMap,0L,Colors,&COLOR_RGB [1L]);
  329.  
  330.                                                             RedLevel   =  (COLOR_RGB [ColorBase]      >> 24L);
  331.  
  332.                                                             GreenLevel =  (COLOR_RGB [ColorBase + 1L] >> 24L);
  333.  
  334.                                                             BlueLevel  =  (COLOR_RGB [ColorBase + 2L] >> 24L);
  335.  
  336.                                                             GT_SetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,(WORD) RedLevel);
  337.  
  338.                                                             GT_SetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,(WORD) GreenLevel);
  339.  
  340.                                                             GT_SetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,(WORD) BlueLevel);
  341.  
  342.                                                             break;
  343.  
  344.                                                  case 'E' : RedLevel = COLOR_RGB [ColorBase] >> 24L;
  345.  
  346.                                                             if (RedLevel > MINVALUE)
  347.                                                             {
  348.                                                                KeepPalette (Win);
  349.  
  350.                                                                RedLevel = (RedLevel < (MINVALUE + DELTA)) ? MINVALUE : RedLevel - DELTA;
  351.  
  352.                                                                GT_SetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,(WORD) RedLevel);
  353.  
  354.                                                                COLOR_RGB [ColorBase] = RedLevel << 24L;
  355.  
  356.                                                                LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  357.                                                             }
  358.  
  359.                                                             break;
  360.  
  361.                                                  case 'e' : RedLevel = COLOR_RGB [ColorBase] >> 24L;
  362.  
  363.                                                             if (RedLevel > MINVALUE)
  364.                                                             {
  365.                                                                KeepPalette (Win);
  366.  
  367.                                                                RedLevel--;
  368.  
  369.                                                                GT_SetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,(WORD) RedLevel);
  370.  
  371.                                                                COLOR_RGB [ColorBase] = RedLevel << 24L;
  372.  
  373.                                                                LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  374.                                                             }
  375.  
  376.                                                             break;
  377.  
  378.                                                  case 'T' : RedLevel = COLOR_RGB [ColorBase] >> 24L;
  379.  
  380.                                                             if (RedLevel < MAXVALUE)
  381.                                                             {
  382.                                                                KeepPalette (Win);
  383.  
  384.                                                                RedLevel += DELTA;
  385.  
  386.                                                                if (RedLevel > MAXVALUE) RedLevel = MAXVALUE;
  387.  
  388.                                                                GT_SetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,(WORD) RedLevel);
  389.  
  390.                                                                COLOR_RGB [ColorBase] = RedLevel << 24L;
  391.  
  392.                                                                LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  393.                                                             }
  394.  
  395.                                                             break;
  396.  
  397.                                                  case 't' : RedLevel = COLOR_RGB [ColorBase] >> 24L;
  398.  
  399.                                                             if (RedLevel < MAXVALUE)
  400.                                                             {
  401.                                                                KeepPalette (Win);
  402.  
  403.                                                                RedLevel++;
  404.  
  405.                                                                GT_SetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,(WORD) RedLevel);
  406.  
  407.                                                                COLOR_RGB [ColorBase] = RedLevel << 24L;
  408.  
  409.                                                                LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  410.                                                             }
  411.  
  412.                                                             break;
  413.  
  414.                                                  case 'F' : GreenLevel = COLOR_RGB [ColorBase + 1L] >> 24L;
  415.  
  416.                                                             if (GreenLevel > MINVALUE)
  417.                                                             {
  418.                                                                KeepPalette (Win);
  419.  
  420.                                                                GreenLevel = (GreenLevel < (MINVALUE + DELTA)) ? MINVALUE : GreenLevel - DELTA;
  421.  
  422.                                                                GT_SetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,(WORD) GreenLevel);
  423.  
  424.                                                                COLOR_RGB [ColorBase + 1L] = GreenLevel << 24L;
  425.  
  426.                                                                LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  427.                                                             }
  428.  
  429.                                                             break;
  430.  
  431.                                                  case 'f' : GreenLevel = COLOR_RGB [ColorBase + 1L] >> 24L;
  432.  
  433.                                                             if (GreenLevel > MINVALUE)
  434.                                                             {
  435.                                                                KeepPalette (Win);
  436.  
  437.                                                                GreenLevel--;
  438.  
  439.                                                                GT_SetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,(WORD) GreenLevel);
  440.  
  441.                                                                COLOR_RGB [ColorBase + 1L] = GreenLevel << 24L;
  442.  
  443.                                                                LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  444.                                                             }
  445.  
  446.                                                             break;
  447.  
  448.                                                  case 'H' : GreenLevel = COLOR_RGB [ColorBase + 1L] >> 24L;
  449.  
  450.                                                             if (GreenLevel < MAXVALUE)
  451.                                                             {
  452.                                                                KeepPalette (Win);
  453.  
  454.                                                                GreenLevel += DELTA;
  455.  
  456.                                                                if (GreenLevel > MAXVALUE) GreenLevel = MAXVALUE;
  457.  
  458.                                                                GT_SetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,(WORD) GreenLevel);
  459.  
  460.                                                                COLOR_RGB [ColorBase + 1L] = GreenLevel << 24L;
  461.  
  462.                                                                LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  463.                                                             }
  464.  
  465.                                                             break;
  466.  
  467.                                                  case 'h' : GreenLevel = COLOR_RGB [ColorBase + 1L] >> 24L;
  468.  
  469.                                                             if (GreenLevel < MAXVALUE)
  470.                                                             {
  471.                                                                KeepPalette (Win);
  472.  
  473.                                                                GreenLevel++;
  474.  
  475.                                                                GT_SetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,(WORD) GreenLevel);
  476.  
  477.                                                                COLOR_RGB [ColorBase + 1L] = GreenLevel << 24L;
  478.  
  479.                                                                LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  480.                                                             }
  481.  
  482.                                                             break;
  483.  
  484.                                                  case 'V' : BlueLevel = COLOR_RGB [ColorBase + 2L] >> 24L;
  485.  
  486.                                                             if (BlueLevel > MINVALUE)
  487.                                                             {
  488.                                                                KeepPalette (Win);
  489.  
  490.                                                                BlueLevel = (BlueLevel < (MINVALUE + DELTA)) ? MINVALUE : BlueLevel - DELTA;
  491.  
  492.                                                                GT_SetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,(WORD) BlueLevel);
  493.  
  494.                                                                COLOR_RGB [ColorBase + 2L] = BlueLevel << 24L;
  495.  
  496.                                                                LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  497.                                                             }
  498.  
  499.                                                             break;
  500.  
  501.                                                  case 'v' : BlueLevel = COLOR_RGB [ColorBase + 2L] >> 24L;
  502.  
  503.                                                             if (BlueLevel > MINVALUE)
  504.                                                             {
  505.                                                                KeepPalette (Win);
  506.  
  507.                                                                BlueLevel--;
  508.  
  509.                                                                GT_SetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,(WORD) BlueLevel);
  510.  
  511.                                                                COLOR_RGB [ColorBase + 2L] = BlueLevel << 24L;
  512.  
  513.                                                                LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  514.                                                             }
  515.  
  516.                                                             break;
  517.  
  518.                                                  case 'N' : BlueLevel = COLOR_RGB [ColorBase + 2L] >> 24L;
  519.  
  520.                                                             if (BlueLevel < MAXVALUE)
  521.                                                             {
  522.                                                                KeepPalette (Win);
  523.  
  524.                                                                BlueLevel += DELTA;
  525.  
  526.                                                                if (BlueLevel > MAXVALUE) BlueLevel = MAXVALUE;
  527.  
  528.                                                                GT_SetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,(WORD) BlueLevel);
  529.  
  530.                                                                COLOR_RGB [ColorBase + 2L] = BlueLevel << 24L;
  531.  
  532.                                                                LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  533.                                                             }
  534.  
  535.                                                             break;
  536.  
  537.                                                  case 'n' : BlueLevel = COLOR_RGB [ColorBase + 2L] >> 24L;
  538.  
  539.                                                             if (BlueLevel < MAXVALUE)
  540.                                                             {
  541.                                                                KeepPalette (Win);
  542.  
  543.                                                                BlueLevel++;
  544.  
  545.                                                                GT_SetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,(WORD) BlueLevel);
  546.  
  547.                                                                COLOR_RGB [ColorBase + 2L] = BlueLevel << 24L;
  548.  
  549.                                                                LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  550.                                                             }
  551.  
  552.                                                             break;
  553.  
  554.                                                  case 'I' :
  555.                                                  case 'i' : KeepPalette (Win);
  556.  
  557.                                                             InvertPalette (Win,STARTPEN,Colors - 1L);
  558.  
  559.                                                             RedLevel   =  (COLOR_RGB [ColorBase]      >> 24L);
  560.  
  561.                                                             GreenLevel =  (COLOR_RGB [ColorBase + 1L] >> 24L);
  562.  
  563.                                                             BlueLevel  =  (COLOR_RGB [ColorBase + 2L] >> 24L);
  564.  
  565.                                                             GT_SetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,(WORD) RedLevel);
  566.  
  567.                                                             GT_SetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,(WORD) GreenLevel);
  568.  
  569.                                                             GT_SetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,(WORD) BlueLevel);
  570.  
  571.                                                             break;
  572.                                          }
  573.  
  574.                                          break;
  575.  
  576.               case IDCMP_GADGETUP      : switch (MyGad->GadgetID)
  577.                                          {
  578.                                                  case ACCEPT  : GetRGB32 (ViewPortAddress (ColorWin)->ColorMap,0L,Colors,&Palette32 [1L]);
  579.  
  580.                                                                 Exit = TRUE;
  581.  
  582.                                                                 break;
  583.  
  584.                                                  case RESET   : KeepPalette (Win);
  585.  
  586.                                                                 LoadRGB32 (ViewPortAddress (ColorWin),Palette32);
  587.  
  588.                                                                 GetRGB32 (ViewPortAddress (ColorWin)->ColorMap,0L,Colors,&COLOR_RGB [1L]);
  589.  
  590.                                                                 RedLevel   =  (COLOR_RGB [ColorBase]      >> 24L);
  591.  
  592.                                                                 GreenLevel =  (COLOR_RGB [ColorBase + 1L] >> 24L);
  593.  
  594.                                                                 BlueLevel  =  (COLOR_RGB [ColorBase + 2L] >> 24L);
  595.  
  596.                                                                 GT_SetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,(WORD) RedLevel);
  597.  
  598.                                                                 GT_SetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,(WORD) GreenLevel);
  599.  
  600.                                                                 GT_SetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,(WORD) BlueLevel);
  601.  
  602.                                                                 break;
  603.  
  604.                                                  case CANCEL  : LoadRGB32 (ViewPortAddress (ColorWin),Palette32);
  605.  
  606.                                                                 Exit = TRUE;
  607.  
  608.                                                                 break;
  609.  
  610.                                                  case COPY    : Copy (ColorWin,SelectedPen);
  611.  
  612.                                                                 Copy_Msg = TRUE;
  613.  
  614.                                                                 Swap_Msg = FALSE;
  615.  
  616.                                                                 Spread_Msg = FALSE;
  617.  
  618.                                                                 break;
  619.  
  620.                                                  case SWAP    : OldPen = SelectedPen;
  621.  
  622.                                                                 Copy_Msg = FALSE;
  623.  
  624.                                                                 Swap_Msg = TRUE;
  625.  
  626.                                                                 Spread_Msg = FALSE;
  627.  
  628.                                                                 break;
  629.  
  630.                                                  case SPREAD  : OldPen = SelectedPen;
  631.  
  632.                                                                 Copy_Msg = FALSE;
  633.  
  634.                                                                 Swap_Msg = FALSE;
  635.  
  636.                                                                 Spread_Msg = TRUE;
  637.         
  638.                                                                 break;
  639.  
  640.                                                  case INVERT  : KeepPalette (Win);
  641.  
  642.                                                                 InvertPalette (Win,STARTPEN,Colors - 1L);
  643.  
  644.                                                                 RedLevel   =  (COLOR_RGB [ColorBase]      >> 24L);
  645.  
  646.                                                                 GreenLevel =  (COLOR_RGB [ColorBase + 1L] >> 24L);
  647.  
  648.                                                                 BlueLevel  =  (COLOR_RGB [ColorBase + 2L] >> 24L);
  649.  
  650.                                                                 GT_SetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,(WORD) RedLevel);
  651.  
  652.                                                                 GT_SetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,(WORD) GreenLevel);
  653.  
  654.                                                                 GT_SetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,(WORD) BlueLevel);
  655.  
  656.                                                                 break;
  657.  
  658.                                                  case UNDO    : LoadRGB32 (ViewPortAddress (ColorWin),UNDO_RGB);
  659.  
  660.                                                                 GetRGB32 (ViewPortAddress (ColorWin)->ColorMap,0L,Colors,&COLOR_RGB [1L]);
  661.  
  662.                                                                 RedLevel   =  (COLOR_RGB [ColorBase]      >> 24L);
  663.  
  664.                                                                 GreenLevel =  (COLOR_RGB [ColorBase + 1L] >> 24L);
  665.  
  666.                                                                 BlueLevel  =  (COLOR_RGB [ColorBase + 2L] >> 24L);
  667.  
  668.                                                                 GT_SetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,(WORD) RedLevel);
  669.  
  670.                                                                 GT_SetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,(WORD) GreenLevel);
  671.  
  672.                                                                 GT_SetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,(WORD) BlueLevel);
  673.  
  674.                                                                 break;
  675.  
  676.                                                  case RED     : KeepPalette (Win);
  677.  
  678.                                                                 GT_GetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,&RedLevel);
  679.  
  680.                                                                 COLOR_RGB [ColorBase]      = RedLevel << 24L;
  681.  
  682.                                                                 LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  683.  
  684.                                                                 break;
  685.  
  686.                                                  case GREEN   : KeepPalette (Win);
  687.  
  688.                                                                 GT_GetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,&GreenLevel);
  689.  
  690.                                                                 COLOR_RGB [ColorBase + 1L] = GreenLevel << 24L;
  691.  
  692.                                                                 LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  693.  
  694.                                                                 break;
  695.  
  696.                                                  case BLUE    : KeepPalette (Win);
  697.  
  698.                                                                 GT_GetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,&BlueLevel);
  699.  
  700.                                                                 COLOR_RGB [ColorBase + 2L] = BlueLevel << 24L;
  701.  
  702.                                                                 LoadRGB32 (ViewPortAddress (ColorWin),COLOR_RGB);
  703.  
  704.                                                                 break;
  705.  
  706.                                                  case PALETTE : GT_GetGadgetAttrs (MyPaletteGad,ColorWin,NULL,GTPA_Color,&SelectedPen);
  707.  
  708.                                                                 if (Copy_Msg)
  709.                                                                 {
  710.                                                                    KeepPalette (Win);
  711.  
  712.                                                                    Paste (ColorWin,SelectedPen);
  713.  
  714.                                                                    Copy_Msg = FALSE;
  715.                                                                 }
  716.  
  717.                                                                 if (Swap_Msg)
  718.                                                                 {
  719.                                                                    KeepPalette (Win);
  720.  
  721.                                                                    NewPen = SelectedPen;
  722.  
  723.                                                                    Swap (ColorWin,OldPen,NewPen);
  724.  
  725.                                                                    Swap_Msg = FALSE;
  726.                                                                 }
  727.  
  728.                                                                 if (Spread_Msg)
  729.                                                                 {
  730.                                                                    KeepPalette (Win);
  731.  
  732.                                                                    NewPen = SelectedPen;
  733.  
  734.                                                                    Spread (ColorWin,OldPen,NewPen);
  735.  
  736.                                                                    Spread_Msg = FALSE;
  737.                                                                 }
  738.  
  739.                                                                 ColorBase = 3L * SelectedPen + 1L;
  740.  
  741.                                                                 RedLevel   =  (COLOR_RGB [ColorBase]      >> 24L);
  742.  
  743.                                                                 GreenLevel =  (COLOR_RGB [ColorBase + 1L] >> 24L);
  744.  
  745.                                                                 BlueLevel  =  (COLOR_RGB [ColorBase + 2L] >> 24L);
  746.  
  747.                                                                 GT_SetGadgetAttrs (RedSliderGad,ColorWin,NULL,GTSL_Level,(WORD) RedLevel);
  748.  
  749.                                                                 GT_SetGadgetAttrs (GreenSliderGad,ColorWin,NULL,GTSL_Level,(WORD) GreenLevel);
  750.  
  751.                                                                 GT_SetGadgetAttrs (BlueSliderGad,ColorWin,NULL,GTSL_Level,(WORD) BlueLevel);
  752.  
  753.                                                                 break;
  754.                                          }
  755.  
  756.                                          break;
  757.  
  758.               case IDCMP_CLOSEWINDOW   : Exit = TRUE;
  759.  
  760.                                          break;
  761.        }
  762.  
  763.      } while (Exit == FALSE);
  764.  
  765.   CloseWindow (ColorWin);
  766.  
  767.   FreeGadgets (GadList);
  768.  
  769.   FreeVisualInfo (VInfo);
  770.  
  771.   return TRUE;
  772. }
  773.  
  774. VOID Copy (struct Window *Win,ULONG PenNumber)
  775. {
  776.   GetRGB32 (ViewPortAddress (Win)->ColorMap,PenNumber,1L,COPY_RGB);
  777. }
  778.  
  779. VOID Paste (struct Window *Win,const ULONG PenNumber)
  780. {
  781. const ULONG ColorBase = 3L * PenNumber + 1L;
  782.  
  783. ULONG Index;
  784.  
  785.   for (Index = 0L; Index < 3L; Index++) COLOR_RGB [ColorBase + Index] = COPY_RGB [Index];
  786.  
  787.   LoadRGB32 (ViewPortAddress (Win),COLOR_RGB);
  788. }
  789.  
  790. VOID Swap (struct Window *Win,const ULONG OldPen,const ULONG NewPen)
  791. {
  792. ULONG Tmp_RGB [3L];
  793.  
  794. ULONG Index;
  795.  
  796.    GetRGB32 (ViewPortAddress (Win)->ColorMap,OldPen,1L,Tmp_RGB);
  797.  
  798.    Copy (Win,NewPen);
  799.  
  800.    Paste (Win,OldPen);
  801.  
  802.    for (Index = 0L; Index < 3L; Index++) COPY_RGB [Index] = Tmp_RGB [Index];
  803.  
  804.    Paste (Win,NewPen);
  805. }
  806.  
  807. BOOL Spread (struct Window *Win,const ULONG OldPen,const ULONG NewPen)
  808. {
  809. const ULONG StartPen = min (OldPen,NewPen) , EndPen = max (OldPen,NewPen);
  810.  
  811. const ULONG  Range   = EndPen - StartPen;
  812.  
  813. LONG RedStep,GreenStep,BlueStep;
  814.  
  815. ULONG Index,RedLevel,GreenLevel,BlueLevel,ColorBase;
  816.  
  817.   if (Range < 2L) return TRUE;
  818.  
  819.   ColorBase   = 3L * StartPen + 1L;
  820.  
  821.   RedLevel    = COLOR_RGB [ColorBase]      >> 8L;
  822.  
  823.   GreenLevel  = COLOR_RGB [ColorBase + 1L] >> 8L;
  824.  
  825.   BlueLevel   = COLOR_RGB [ColorBase + 2L] >> 8L;
  826.  
  827.   ColorBase   = 3L * EndPen + 1L;
  828.  
  829.   RedStep     = (LONG) (COLOR_RGB [ColorBase]      >> 8L);
  830.  
  831.   GreenStep   = (LONG) (COLOR_RGB [ColorBase + 1L] >> 8L);
  832.  
  833.   BlueStep    = (LONG) (COLOR_RGB [ColorBase + 2L] >> 8L);
  834.  
  835.   RedStep    -= (LONG) RedLevel;
  836.  
  837.   GreenStep  -= (LONG) GreenLevel;
  838.  
  839.   BlueStep   -= (LONG) BlueLevel;
  840.  
  841.   RedStep    /= (LONG) Range;
  842.  
  843.   GreenStep  /= (LONG) Range;
  844.  
  845.   BlueStep   /= (LONG) Range;
  846.  
  847.   for (Index = (StartPen + 1L); Index < EndPen; Index++)
  848.   {
  849.       RedLevel   += RedStep;
  850.  
  851.       GreenLevel += GreenStep;
  852.  
  853.       BlueLevel  += BlueStep;
  854.  
  855.       ColorBase   = 3L * Index + 1L;
  856.  
  857.       COLOR_RGB [ColorBase]      = RedLevel   << 8L;
  858.  
  859.       COLOR_RGB [ColorBase + 1L] = GreenLevel << 8L;
  860.  
  861.       COLOR_RGB [ColorBase + 2L] = BlueLevel  << 8L;
  862.   }
  863.  
  864.   LoadRGB32 (ViewPortAddress (Win),COLOR_RGB);
  865.  
  866.   return FALSE;
  867. }
  868.  
  869. VOID KeepPalette (struct Window *Win)
  870. {
  871. ULONG Colors;
  872.  
  873.   Colors = (CheckEHB (GetVPModeID (ViewPortAddress (Win))) ? 32L : 1L << Win->RPort->BitMap->Depth);
  874.  
  875.   GetRGB32 (ViewPortAddress (Win)->ColorMap,0L,Colors,&UNDO_RGB [1L]);
  876. }
  877.  
  878. VOID InvertPalette (struct Window *Win,ULONG StartPen,ULONG EndPen)
  879. {
  880.   while (StartPen < EndPen) Swap (Win,StartPen++,EndPen--);
  881. }
  882.